
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Four Operations 2 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="four-operations-3.html" />
    
    
    <link rel="prev" href="four-operations-1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    前端进阶
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Grid System.html">
            
                <a href="Grid System.html">
            
                    
                    Grid System
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="Handwritten signature.html">
            
                <a href="Handwritten signature.html">
            
                    
                    Handwritten Signature
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Mobile Soft Keyboard.html">
            
                <a href="Mobile Soft Keyboard.html">
            
                    
                    Mobile Soft Keyboard
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="authentication.html">
            
                <a href="authentication.html">
            
                    
                    Authentication
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    Code
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" >
            
                <span>
            
                    
                    Four Operations
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="control loading.html">
            
                <a href="control loading.html">
            
                    
                    Control Loading
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="downfile.html">
            
                <a href="downfile.html">
            
                    
                    Downfile
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="dynamic-routing.html">
            
                <a href="dynamic-routing.html">
            
                    
                    Dynamic Routing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="ecs.html">
            
                <a href="ecs.html">
            
                    
                    Ecs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="engineering.html">
            
                <a href="engineering.html">
            
                    
                    Engineering
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="eslint-vscode-format.html">
            
                <a href="eslint-vscode-format.html">
            
                    
                    Eslint Vscode Format
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="eventloop.html">
            
                <a href="eventloop.html">
            
                    
                    Eventloop
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="four-operations-1.html">
            
                <a href="four-operations-1.html">
            
                    
                    Four Operations 1
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.16" data-path="four-operations-2.html">
            
                <a href="four-operations-2.html">
            
                    
                    Four Operations 2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="four-operations-3.html">
            
                <a href="four-operations-3.html">
            
                    
                    Four Operations 3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="four-operations-4.html">
            
                <a href="four-operations-4.html">
            
                    
                    Four Operations 4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="front end summary.html">
            
                <a href="front end summary.html">
            
                    
                    Front End Summary
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="git commit style.html">
            
                <a href="git commit style.html">
            
                    
                    Git Commit Style
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="git-commands.html">
            
                <a href="git-commands.html">
            
                    
                    Git Commands
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="high-quality-code.html">
            
                <a href="high-quality-code.html">
            
                    
                    High Quality Code
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="history.html">
            
                <a href="history.html">
            
                    
                    History
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="html-tag.html">
            
                <a href="html-tag.html">
            
                    
                    Html Tag
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="http-https-http2.html">
            
                <a href="http-https-http2.html">
            
                    
                    Http Https Http2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="ignore log.html">
            
                <a href="ignore log.html">
            
                    
                    Ignore Log
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" >
            
                <span>
            
                    
                    Imgs
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="javascript-optimization.html">
            
                <a href="javascript-optimization.html">
            
                    
                    Javascript Optimization
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="local application.html">
            
                <a href="local application.html">
            
                    
                    Local Application
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="locality.html">
            
                <a href="locality.html">
            
                    
                    Locality
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="loop-unrolling.html">
            
                <a href="loop-unrolling.html">
            
                    
                    Loop Unrolling
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="mobile recommendation.html">
            
                <a href="mobile recommendation.html">
            
                    
                    Mobile Recommendation
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="modal.html">
            
                <a href="modal.html">
            
                    
                    Modal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="monitor.html">
            
                <a href="monitor.html">
            
                    
                    Monitor
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="performance.html">
            
                <a href="performance.html">
            
                    
                    Performance
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="print-talbe.html">
            
                <a href="print-talbe.html">
            
                    
                    Print Talbe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.37" data-path="promise.html">
            
                <a href="promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="teaching.html">
            
                <a href="teaching.html">
            
                    
                    Teaching
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.39" data-path="vue refresh.html">
            
                <a href="vue refresh.html">
            
                    
                    Vue Refresh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.40" data-path="vue-page-not-refresh.html">
            
                <a href="vue-page-not-refresh.html">
            
                    
                    Vue Page Not Refresh
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.41" data-path="why-learn-theory.html">
            
                <a href="why-learn-theory.html">
            
                    
                    Why Learn Theory
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Four Operations 2</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528;-javascript-&#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x4E8C;&#xFF09;&#x8BED;&#x6CD5;&#x5206;&#x6790;">&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528; JavaScript &#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x4E8C;&#xFF09;&#x8BED;&#x6CD5;&#x5206;&#x6790;</h2>
<h3 id="&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7684;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#xFF08;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#x662F;&#x5206;&#x5C42;&#x7684;&#xFF09;">&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7684;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#xFF08;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#x662F;&#x5206;&#x5C42;&#x7684;&#xFF09;</h3>
<ol>
<li>x* &#x8868;&#x793A; x &#x51FA;&#x73B0;&#x96F6;&#x6B21;&#x6216;&#x591A;&#x6B21;</li>
<li>x | y &#x8868;&#x793A; x &#x6216; y &#x5C06;&#x51FA;&#x73B0;</li>
<li>( ) &#x5706;&#x62EC;&#x53F7;&#xFF0C;&#x7528;&#x4E8E;&#x8BED;&#x8A00;&#x6784;&#x8BCD;&#x7684;&#x5206;&#x7EC4;</li>
</ol>
<p>&#x4EE5;&#x4E0B;&#x89C4;&#x5219;&#x4ECE;&#x5DE6;&#x5F80;&#x53F3;&#x770B;&#xFF0C;&#x8868;&#x793A;&#x5DE6;&#x8FB9;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x8FD8;&#x80FD;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x7EC6;&#x5206;&#x6210;&#x53F3;&#x8FB9;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4E00;&#x76F4;&#x7EC6;&#x5206;&#x5230;&#x4E0D;&#x53EF;&#x518D;&#x5206;&#x4E3A;&#x6B62;&#x3002;</p>
<ul>
<li><strong>expression</strong>: addExpression</li>
<li><strong>addExpression</strong>: mulExpression (op mulExpression)*</li>
<li><strong>mulExpression</strong>: term (op term)*</li>
<li><strong>term</strong>: &apos;(&apos; expression &apos;)&apos; | integerConstant</li>
<li><strong>op</strong>: <code>+ - * /</code></li>
</ul>
<p>PS: <code>addExpression</code> &#x5BF9;&#x5E94; <code>+</code> <code>-</code> &#x8868;&#x8FBE;&#x5F0F;&#xFF0C;<code>mulExpression</code> &#x5BF9;&#x5E94; <code>*</code> <code>/</code> &#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;&#x5206;&#x6790;">&#x8BED;&#x6CD5;&#x5206;&#x6790;</h3>
<p>&#x5BF9;&#x8F93;&#x5165;&#x7684;&#x6587;&#x672C;&#x6309;&#x7167;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#x8FDB;&#x884C;&#x5206;&#x6790;&#x5E76;&#x786E;&#x5B9A;&#x5176;&#x8BED;&#x6CD5;&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x79CD;&#x8FC7;&#x7A0B;&#xFF0C;&#x79F0;&#x4E3A;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x3002;</p>
<p>&#x4E00;&#x822C;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x7684;&#x8F93;&#x51FA;&#x4E3A;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#xFF08;AST&#xFF09;&#x6216;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x6811;&#xFF08;parse tree&#xFF09;&#x3002;&#x4F46;&#x7531;&#x4E8E;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x91C7;&#x53D6;&#x7684;&#x65B9;&#x6848;&#x662F;&#x5373;&#x65F6;&#x5730;&#x8FDB;&#x884C;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x548C;&#x9519;&#x8BEF;&#x62A5;&#x544A;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x4E0D;&#x9700;&#x8981;&#x5728;&#x5185;&#x5B58;&#x4E2D;&#x4FDD;&#x5B58;&#x6574;&#x4E2A;&#x7A0B;&#x5E8F;&#x7ED3;&#x6784;&#x3002;</p>
<p>&#x5148;&#x6765;&#x770B;&#x770B;&#x600E;&#x4E48;&#x5206;&#x6790;&#x4E00;&#x4E2A;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x8868;&#x8FBE;&#x5F0F; <code>1 + 2 * 3</code>&#x3002;</p>
<p>&#x9996;&#x5148;&#x5339;&#x914D;&#x7684;&#x662F; <code>expression</code>&#xFF0C;&#x7531;&#x4E8E;&#x76EE;&#x524D; <code>expression</code> &#x5F80;&#x4E0B;&#x5206;&#x53EA;&#x6709;&#x4E00;&#x79CD;&#x53EF;&#x80FD;&#xFF0C;&#x5373;  <code>addExpression</code>&#xFF0C;&#x6240;&#x4EE5;&#x5206;&#x89E3;&#x4E3A; <code>addExpression</code>&#x3002;
&#x4F9D;&#x6B21;&#x7C7B;&#x63A8;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x987A;&#x5E8F;&#x4E3A; <code>mulExpression</code>&#x3001;<code>term</code>&#x3001;<code>1</code>&#xFF08;integerConstant&#xFF09;&#x3001;<code>+</code>&#xFF08;op&#xFF09;&#x3001;<code>mulExpression</code>&#x3001;<code>term</code>&#x3001;<code>2</code>&#xFF08;integerConstant&#xFF09;&#x3001;<code>*</code>&#xFF08;op&#xFF09;&#x3001;<code>mulExpression</code>&#x3001;<code>term</code>&#x3001;<code>3</code>&#xFF08;integerConstant&#xFF09;&#x3002;</p>
<p>&#x5982;&#x4E0B;&#x56FE;&#x6240;&#x793A;&#xFF1A;</p>
<p><img src="https://github.com/woai3c/Front-end-articles/blob/master/imgs/four-operation.jpg" alt="img"></p>
<p>&#x8FD9;&#x91CC;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x4EBA;&#x6709;&#x7591;&#x95EE;&#xFF0C;&#x4E3A;&#x4EC0;&#x4E48;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x641E;&#x5F97;&#x8FD9;&#x4E48;&#x590D;&#x6742;&#xFF0C;<code>expression</code> &#x4E0B;&#x9762;&#x6709; <code>addExpression</code>&#xFF0C;<code>addExpression</code> &#x4E0B;&#x9762;&#x8FD8;&#x6709; <code>mulExpression</code>&#x3002;
&#x5176;&#x5B9E;&#x8FD9;&#x91CC;&#x662F;&#x4E3A;&#x4E86;&#x8003;&#x8651;&#x8FD0;&#x7B97;&#x7B26;&#x4F18;&#x5148;&#x7EA7;&#x800C;&#x8BBE;&#x7684;&#xFF0C;<code>mulExpr</code> &#x6BD4; <code>addExpr</code> &#x8868;&#x8FBE;&#x5F0F;&#x8FD0;&#x7B97;&#x7EA7;&#x8981;&#x9AD8;&#x3002;</p>
<pre><code>1 + 2 * 3
compileExpression
&#x2002;&#x2002;&#x2002;|&#x2002;compileAddExpr
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;compileMultExpr
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;compileTerm
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|_&#x2009;matches integerConstant        push 1
  &#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|_
&#x2002; &#x2002;|&#x2002;&#x2002;|&#x2002;matches &apos;+&apos;
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;compileMultExpr
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;compileTerm
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|_&#x2009;matches integerConstant        push 2
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;matches &apos;*&apos;
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;compileTerm
 &#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|_&#x2009;matches integerConstant        push 3
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|&#x2002;&#x2002;|_&#x2009;compileOp(&apos;*&apos;)                      *
&#x2002;&#x2002;&#x2002;|&#x2002;&#x2002;|_&#x2009;compileOp(&apos;+&apos;)                         +
&#x2002;  |_
</code></pre><p> &#x6709;&#x5F88;&#x591A;&#x7B97;&#x6CD5;&#x53EF;&#x7528;&#x6765;&#x6784;&#x5EFA;&#x8BED;&#x6CD5;&#x5206;&#x6790;&#x6811;&#xFF0C;&#x8FD9;&#x91CC;&#x53EA;&#x8BB2;&#x4E24;&#x79CD;&#x7B97;&#x6CD5;&#x3002;</p>
<h4 id="&#x9012;&#x5F52;&#x4E0B;&#x964D;&#x5206;&#x6790;&#x6CD5;">&#x9012;&#x5F52;&#x4E0B;&#x964D;&#x5206;&#x6790;&#x6CD5;</h4>
<p>&#x9012;&#x5F52;&#x4E0B;&#x964D;&#x5206;&#x6790;&#x6CD5;&#xFF0C;&#x4E5F;&#x79F0;&#x4E3A;&#x81EA;&#x9876;&#x5411;&#x4E0B;&#x5206;&#x6790;&#x6CD5;&#x3002;&#x6309;&#x7167;&#x8BED;&#x6CD5;&#x89C4;&#x5219;&#x4E00;&#x6B65;&#x6B65;&#x9012;&#x5F52;&#x5730;&#x5206;&#x6790; token &#x6D41;&#xFF0C;&#x5982;&#x679C;&#x9047;&#x5230;&#x975E;&#x7EC8;&#x7ED3;&#x7B26;&#xFF0C;&#x5219;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x5206;&#x6790;&#xFF0C;&#x76F4;&#x5230;&#x7EC8;&#x7ED3;&#x7B26;&#x4E3A;&#x6B62;&#x3002;</p>
<h4 id="ll0&#x5206;&#x6790;&#x6CD5;">LL(0)&#x5206;&#x6790;&#x6CD5;</h4>
<p>&#x9012;&#x5F52;&#x4E0B;&#x964D;&#x5206;&#x6790;&#x6CD5;&#x662F;&#x7B80;&#x5355;&#x9AD8;&#x6548;&#x7684;&#x7B97;&#x6CD5;&#xFF0C;LL(0)&#x5728;&#x6B64;&#x57FA;&#x7840;&#x4E0A;&#x591A;&#x4E86;&#x4E00;&#x4E2A;&#x6B65;&#x9AA4;&#xFF0C;&#x5F53;&#x7B2C;&#x4E00;&#x4E2A; token &#x4E0D;&#x8DB3;&#x4EE5;&#x786E;&#x5B9A;&#x5143;&#x7D20;&#x7C7B;&#x578B;&#x65F6;&#xFF0C;&#x5BF9;&#x4E0B;&#x4E00;&#x4E2A;&#x5B57;&#x5143;&#x91C7;&#x53D6;&#x201C;&#x63D0;&#x524D;&#x67E5;&#x770B;&#x201D;&#xFF0C;&#x6709;&#x53EF;&#x80FD;&#x4F1A;&#x89E3;&#x51B3;&#x8FD9;&#x79CD;&#x4E0D;&#x786E;&#x5B9A;&#x6027;&#x3002;</p>
<p>&#x4EE5;&#x4E0A;&#x662F;&#x5BF9;&#x8FD9;&#x4E24;&#x79CD;&#x7B97;&#x6CD5;&#x7684;&#x7B80;&#x4ECB;&#xFF0C;&#x5177;&#x4F53;&#x5B9E;&#x73B0;&#x8BF7;&#x770B;&#x4E0B;&#x65B9;&#x7684;&#x4EE3;&#x7801;&#x5B9E;&#x73B0;&#x3002;</p>
<h3 id="&#x8868;&#x8FBE;&#x5F0F;&#x4EE3;&#x7801;&#x751F;&#x6210;">&#x8868;&#x8FBE;&#x5F0F;&#x4EE3;&#x7801;&#x751F;&#x6210;</h3>
<p>&#x6211;&#x4EEC;&#x901A;&#x5E38;&#x7528;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x4E2D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x4E8E;&#x8BA1;&#x7B97;&#x673A;&#x6765;&#x8BF4;&#x4E2D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x4E0D;&#x4FBF;&#x4E8E;&#x8BA1;&#x7B97;&#x3002;&#x6240;&#x4EE5;&#x5728;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x9636;&#x6BB5;&#xFF0C;&#x8981;&#x5C06;&#x4E2D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x8F6C;&#x6362;&#x4E3A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p><strong>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;</strong></p>
<p>&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x53C8;&#x79F0;&#x9006;&#x6CE2;&#x5170;&#x5F0F;&#xFF0C;&#x6307;&#x7684;&#x662F;&#x4E0D;&#x5305;&#x542B;&#x62EC;&#x53F7;&#xFF0C;&#x8FD0;&#x7B97;&#x7B26;&#x653E;&#x5728;&#x4E24;&#x4E2A;&#x8FD0;&#x7B97;&#x5BF9;&#x8C61;&#x7684;&#x540E;&#x9762;&#xFF0C;&#x6240;&#x6709;&#x7684;&#x8BA1;&#x7B97;&#x6309;&#x8FD0;&#x7B97;&#x7B26;&#x51FA;&#x73B0;&#x7684;&#x987A;&#x5E8F;&#xFF0C;&#x4E25;&#x683C;&#x4ECE;&#x5DE6;&#x5411;&#x53F3;&#x8FDB;&#x884C;&#xFF08;&#x4E0D;&#x518D;&#x8003;&#x8651;&#x8FD0;&#x7B97;&#x7B26;&#x7684;&#x4F18;&#x5148;&#x89C4;&#x5219;&#xFF09;&#x3002;</p>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<p>&#x4E2D;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A; <code>5 + 5</code> &#x8F6C;&#x6362;&#x4E3A;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;<code>5 5 +</code>&#xFF0C;&#x7136;&#x540E;&#x518D;&#x6839;&#x636E;&#x540E;&#x7F00;&#x8868;&#x8FBE;&#x5F0F;&#x751F;&#x6210;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// 5 + 5 &#x8F6C;&#x6362;&#x4E3A; 5 5 + &#x518D;&#x751F;&#x6210;&#x4EE3;&#x7801;</span>
push <span class="hljs-number">5</span>
push <span class="hljs-number">5</span>
add
</code></pre>
<h3 id="&#x4EE3;&#x7801;&#x5B9E;&#x73B0;">&#x4EE3;&#x7801;&#x5B9E;&#x73B0;</h3>
<p>&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x7684;&#x7406;&#x8BBA;&#x77E5;&#x8BC6;&#x50CF;&#x5929;&#x4E66;&#xFF0C;&#x7ECF;&#x5E38;&#x8BA9;&#x4EBA;&#x770B;&#x5F97;&#x4E91;&#x91CC;&#x96FE;&#x91CC;&#xFF0C;&#x4F46;&#x771F;&#x6B63;&#x52A8;&#x624B;&#x505A;&#x8D77;&#x6765;&#xFF0C;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;&#x5176;&#x5B9E;&#x8FD8;&#x633A;&#x7B80;&#x5355;&#x7684;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4E0A;&#x9762;&#x7684;&#x7406;&#x8BBA;&#x77E5;&#x8BC6;&#x770B;&#x4E0D;&#x592A;&#x61C2;&#xFF0C;&#x6CA1;&#x5173;&#x7CFB;&#xFF0C;&#x5148;&#x770B;&#x4EE3;&#x7801;&#xFF0C;&#x518D;&#x548C;&#x7406;&#x8BBA;&#x77E5;&#x8BC6;&#x7ED3;&#x5408;&#x8D77;&#x6765;&#x770B;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5F15;&#x5165;&#x4E0A;&#x4E00;&#x7BC7;&#x6587;&#x7AE0;&#x8BCD;&#x6CD5;&#x5206;&#x6790;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-comment">// &#x6C47;&#x7F16;&#x4EE3;&#x7801;&#x751F;&#x6210;&#x5668;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AssemblyWriter</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">this</span>.output = <span class="hljs-string">&apos;&apos;</span>
}

AssemblyWriter.prototype = {
    writePush(digit) {
        <span class="hljs-keyword">this</span>.output += <span class="hljs-string">`push <span class="hljs-subst">${digit}</span>\r\n`</span>
    },

    writeOP(op) {
        <span class="hljs-keyword">this</span>.output += op + <span class="hljs-string">&apos;\r\n&apos;</span>
    },

    <span class="hljs-comment">//&#x8F93;&#x51FA;&#x6C47;&#x7F16;&#x4EE3;&#x7801;</span>
    outputStr() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.output
    }
}

<span class="hljs-comment">// &#x8BED;&#x6CD5;&#x5206;&#x6790;&#x5668;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Parser</span>(<span class="hljs-params">tokens, writer</span>) </span>{
    <span class="hljs-keyword">this</span>.writer = writer
    <span class="hljs-keyword">this</span>.tokens = tokens
    <span class="hljs-comment">// tokens &#x6570;&#x7EC4;&#x7D22;&#x5F15;</span>
    <span class="hljs-keyword">this</span>.i = <span class="hljs-number">-1</span>
    <span class="hljs-keyword">this</span>.opMap1 = {
        <span class="hljs-string">&apos;+&apos;</span>: <span class="hljs-string">&apos;add&apos;</span>,
        <span class="hljs-string">&apos;-&apos;</span>: <span class="hljs-string">&apos;sub&apos;</span>,
    }

    <span class="hljs-keyword">this</span>.opMap2 = {
        <span class="hljs-string">&apos;/&apos;</span>: <span class="hljs-string">&apos;div&apos;</span>,
        <span class="hljs-string">&apos;*&apos;</span>: <span class="hljs-string">&apos;mul&apos;</span>
    }

    <span class="hljs-keyword">this</span>.init()
}

Parser.prototype = {
    init() {
        <span class="hljs-keyword">this</span>.compileExpression()
    },

    compileExpression() {
        <span class="hljs-keyword">this</span>.compileAddExpr()
    },

    compileAddExpr() {
        <span class="hljs-keyword">this</span>.compileMultExpr()
        <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
            <span class="hljs-keyword">this</span>.getNextToken()
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.opMap1[<span class="hljs-keyword">this</span>.token]) {
                <span class="hljs-keyword">let</span> op = <span class="hljs-keyword">this</span>.opMap1[<span class="hljs-keyword">this</span>.token]
                <span class="hljs-keyword">this</span>.compileMultExpr()
                <span class="hljs-keyword">this</span>.writer.writeOP(op)
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-comment">// &#x6CA1;&#x6709;&#x5339;&#x914D;&#x4E0A;&#x76F8;&#x5E94;&#x7684;&#x64CD;&#x4F5C;&#x7B26; &#x8FD9;&#x91CC;&#x4E3A;&#x6CA1;&#x6709;&#x5339;&#x914D;&#x4E0A; + - </span>
                <span class="hljs-comment">// &#x5C06; token &#x7D22;&#x5F15;&#x540E;&#x9000;&#x4E00;&#x4F4D;</span>
                <span class="hljs-keyword">this</span>.i--
                <span class="hljs-keyword">break</span>
            }
        }
    },

    compileMultExpr() {
        <span class="hljs-keyword">this</span>.compileTerm()
        <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
            <span class="hljs-keyword">this</span>.getNextToken()
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.opMap2[<span class="hljs-keyword">this</span>.token]) {
                <span class="hljs-keyword">let</span> op = <span class="hljs-keyword">this</span>.opMap2[<span class="hljs-keyword">this</span>.token]
                <span class="hljs-keyword">this</span>.compileTerm()
                <span class="hljs-keyword">this</span>.writer.writeOP(op)
            } <span class="hljs-keyword">else</span> {
                <span class="hljs-comment">// &#x6CA1;&#x6709;&#x5339;&#x914D;&#x4E0A;&#x76F8;&#x5E94;&#x7684;&#x64CD;&#x4F5C;&#x7B26; &#x8FD9;&#x91CC;&#x4E3A;&#x6CA1;&#x6709;&#x5339;&#x914D;&#x4E0A; * / </span>
                <span class="hljs-comment">// &#x5C06; token &#x7D22;&#x5F15;&#x540E;&#x9000;&#x4E00;&#x4F4D;</span>
                <span class="hljs-keyword">this</span>.i--
                <span class="hljs-keyword">break</span>
            }
        }
    },

    compileTerm() {
        <span class="hljs-keyword">this</span>.getNextToken()
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.token == <span class="hljs-string">&apos;(&apos;</span>) {
            <span class="hljs-keyword">this</span>.compileExpression()
            <span class="hljs-keyword">this</span>.getNextToken()
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.token != <span class="hljs-string">&apos;)&apos;</span>) {
                <span class="hljs-keyword">throw</span> <span class="hljs-string">&apos;&#x7F3A;&#x5C11;&#x53F3;&#x62EC;&#x53F7;&#xFF1A;)&apos;</span>
            }
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-regexp">/^\d+$/</span>.test(<span class="hljs-keyword">this</span>.token)) {
            <span class="hljs-keyword">this</span>.writer.writePush(<span class="hljs-keyword">this</span>.token)
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">throw</span> <span class="hljs-string">&apos;&#x9519;&#x8BEF;&#x7684; token&#xFF1A;&#x7B2C; &apos;</span> + (<span class="hljs-keyword">this</span>.i + <span class="hljs-number">1</span>) + <span class="hljs-string">&apos; &#x4E2A; token (&apos;</span> + <span class="hljs-keyword">this</span>.token + <span class="hljs-string">&apos;)&apos;</span>
        }
    },

    getNextToken() {
        <span class="hljs-keyword">this</span>.token = <span class="hljs-keyword">this</span>.tokens[++<span class="hljs-keyword">this</span>.i]
    },

    getInstructions() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.writer.outputStr()
    }
}

<span class="hljs-keyword">const</span> tokens = lexicalAnalysis(<span class="hljs-string">&apos;100+10*10&apos;</span>)
<span class="hljs-keyword">const</span> writer = <span class="hljs-keyword">new</span> AssemblyWriter()
<span class="hljs-keyword">const</span> parser = <span class="hljs-keyword">new</span> Parser(tokens, writer)
<span class="hljs-keyword">const</span> instructions = parser.getInstructions()
<span class="hljs-built_in">console</span>.log(instructions) <span class="hljs-comment">// &#x8F93;&#x51FA;&#x751F;&#x6210;&#x7684;&#x6C47;&#x7F16;&#x4EE3;&#x7801;</span>
<span class="hljs-comment">/*
push 100
push 10
push 10
mul
add
*/</span>
</code></pre>
<ul>
<li><a href="https://github.com/woai3c/Front-end-articles/blob/master/four-operations-1.md" target="_blank">&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528; JavaScript &#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x4E00;&#xFF09;&#x8BCD;&#x6CD5;&#x5206;&#x6790;</a></li>
<li><a href="https://github.com/woai3c/Front-end-articles/blob/master/four-operations-2.md" target="_blank">&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528; JavaScript &#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x4E8C;&#xFF09;&#x8BED;&#x6CD5;&#x5206;&#x6790;</a></li>
<li><a href="https://github.com/woai3c/Front-end-articles/blob/master/four-operations-3.md" target="_blank">&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528; JavaScript &#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x4E09;&#xFF09;&#x6A21;&#x62DF;&#x6267;&#x884C;</a></li>
<li><a href="https://github.com/woai3c/Front-end-articles/blob/master/four-operations-4.md" target="_blank">&#x7F16;&#x8BD1;&#x539F;&#x7406;&#x5B9E;&#x6218;&#x5165;&#x95E8;&#xFF1A;&#x7528; JavaScript &#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x56DB;&#x5219;&#x8FD0;&#x7B97;&#x7F16;&#x8BD1;&#x5668;&#xFF08;&#x56DB;&#xFF09;&#x7ED3;&#x8BED;</a></li>
<li><a href="https://github.com/woai3c/Front-end-articles/blob/master/code/four-operations/code.js" target="_blank">&#x5B8C;&#x6574;&#x6E90;&#x7801;</a><h2 id="&#x53C2;&#x8003;&#x8D44;&#x6599;&#xFF1A;&#x8BA1;&#x7B97;&#x673A;&#x7CFB;&#x7EDF;&#x8981;&#x7D20;">&#x53C2;&#x8003;&#x8D44;&#x6599;&#xFF1A;<a href="https://book.douban.com/subject/1998341/" target="_blank">&#x8BA1;&#x7B97;&#x673A;&#x7CFB;&#x7EDF;&#x8981;&#x7D20;</a></h2>
</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="four-operations-1.html" class="navigation navigation-prev " aria-label="Previous page: Four Operations 1">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="four-operations-3.html" class="navigation navigation-next " aria-label="Next page: Four Operations 3">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Four Operations 2","level":"1.16","depth":1,"next":{"title":"Four Operations 3","level":"1.17","depth":1,"path":"前端进阶/four-operations-3.md","ref":"前端进阶/four-operations-3.md","articles":[]},"previous":{"title":"Four Operations 1","level":"1.15","depth":1,"path":"前端进阶/four-operations-1.md","ref":"前端进阶/four-operations-1.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["summary","livereload"],"pluginsConfig":{"summary":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"前端进阶/four-operations-2.md","mtime":"2021-02-15T12:04:36.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-03-24T08:50:55.666Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

